#include "Animation.h"
void Animation::LoadFrames(const char *filename, int fw, int fh, int numFrames)
{
    BITMAP *img = load_bitmap(filename, 0);
    AnimationFrame *curr;
    w = fw;
    h = fh;
    FirstFrame = new AnimationFrame;
    FirstFrame->sprite = create_bitmap(fw, fh);
    blit(img, FirstFrame->sprite, 0, 0, 0, 0, fw, fh);

    curr = FirstFrame;
    frames = 1;
    for(int i = 1; i < numFrames; i++)
    {
        curr->next = new AnimationFrame;
        curr->next->sprite = create_bitmap(fw, fh);
        blit(img, curr->next->sprite, i*fw, 0, 0, 0, fw, fh);
        curr = curr->next;
        frames++;
    }
    curr->next = NULL;
    CurrentFrame = FirstFrame;
}
void Animation::flipp()
{
    if(flipped)
        flipped = false;
    else
        flipped = true;
}
Animation::Animation()
{
    frames = 0;
    FirstFrame = NULL;
    CurrentFrame = NULL;
    animationSpeed = 10;
    lastUpdate = clock();
    running = false;
}
BITMAP *Animation::getCurrentFrame()
{
    return CurrentFrame->sprite;
}
void Animation::play()
{
    if(FirstFrame)
        running = true;
    finished = false;
}
void Animation::stop()
{
    running = false;
}
void Animation::update()
{
    clock_t now = clock();
    if(running)
    {
        if( (now-lastUpdate)>1000/animationSpeed )
        {
            if(CurrentFrame->next)
                CurrentFrame = CurrentFrame->next;
            else
            {
                CurrentFrame = FirstFrame;
                finished = true;
            }
            lastUpdate = clock();
        }
    }
}
